package com.jinglin.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.j2objc.annotations.AutoreleasePool;
import com.jinglin.mapper.ArticleMapper;
import com.jinglin.pojo.Article;
import com.jinglin.pojo.ArticleCate;
import com.jinglin.service.ArticleCateService;
import com.jinglin.service.ArticleService;
import com.jinglin.service.DashboardService;
import com.jinglin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class DashboardServiceImpl implements DashboardService {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleCateService articleCateService;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Map<String, String> getSiteInfo() {
        // 获取文章总数
        int articleCount = articleService.count();
        // 获取今天新增的文章总数
        AtomicInteger addCount = new AtomicInteger();
        List<Article> list = articleService.list();
        list.forEach(article -> {
            String pub_date = article.getPub_date();
            // 解析数据库中的日期
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzz)", Locale.ENGLISH);
            ZonedDateTime dbDate = ZonedDateTime.parse(pub_date, formatter);

            // 获取今天开始和结束时间
            LocalDate now = LocalDate.now(ZoneId.of("GMT+08:00"));
            ZonedDateTime startOfDay = now.atStartOfDay(ZoneId.of("GMT+08:00"));
            ZonedDateTime endOfDay = now.plusDays(1).atStartOfDay(ZoneId.of("GMT+08:00")).minusNanos(1);

            // 判断数据库中的时间是否在今天范围内
            if (!dbDate.isBefore(startOfDay) && !dbDate.isAfter(endOfDay)) {
                // addCount++ 原子性操作
                addCount.getAndIncrement();
                System.out.println("该时间在今天范围内");
            }
        });

        // 获取用户总数
        int userCount = userService.count();

        //toDo 浏览量（暂时还没有写逻辑）
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("articleCount",String.valueOf(articleCount));
        hashMap.put("addCount",String.valueOf(addCount));
        hashMap.put("userCount",String.valueOf(userCount));
        hashMap.put("totalViews","6666");

        return hashMap;
    }

    @Override
    public List<Map<String, Object>> getArticleCategorySize() {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        List<ArticleCate> articleCateList = articleCateService.list();
        articleCateList.forEach(articleCate -> {
            String id = articleCate.getId();
            Integer articleCount = articleMapper.getArticleByCategory(id);
            HashMap<String, Object> map = new HashMap<>();
            map.put("categoryName",articleCate.getCate_name());
            map.put("articleCount",articleCount);
            list.add(map);
        });

        return list;
    }

    @Override
    public List<Article> getRecommendArticle(Integer number) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getRecommend,1);
        Page<Article> page = new Page<>(0,number);
        Page<Article> page1 = articleService.page(page, wrapper);
        return page1.getRecords();
    }


}
